package cn.fl.notice.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.flow.controller.SysFlowRejRsnController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.fl.notice.service.ISysFlowNoticeCfgService;
import cn.fl.notice.vo.SysFlowNoticeCfgVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.fl.system.constant.SysLogConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;

/**
 * @Description: 流程待办消息通知配置 控制层
 */
@Controller
@RequestMapping("/notice/sysFlowNoticeCfg")
public class SysFlowNoticeCfgController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SysFlowRejRsnController.class);

    /**
     * SysFlowNoticeCfg 服务层BEAN
     */
    @Autowired
    private ISysFlowNoticeCfgService sysFlowNoticeCfgService;


    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("system/notice/sysFlowMsgModify");
        Long userId = CurrentThreadContext.getCurrentUserId();
        String sysCd = CurrentThreadContext.getCurrentSysCd();
        try {
            SysFlowNoticeCfgVO vo = this.sysFlowNoticeCfgService.getSysFlowNoticeCfg(userId, sysCd);
            if (null != vo) {
                view.addObject("flowId", vo.getId());
                view.addObject("isOpen", vo.getIsOpen());
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "toModifyPage", "异常：" + e.getMessage());
            return null;
        }
        BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        String isOpen = request.getParameter("isOpen");
        if (!Integer.toString(BaseConstant.IS_YESNO_YES).equals(isOpen)) {
            isOpen = Integer.toString(BaseConstant.IS_YESNO_NO);
        }

        SysFlowNoticeCfgVO vo = new SysFlowNoticeCfgVO();
        vo.setIsOpen(Integer.parseInt(isOpen));
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);

        try {
            BaseLogUtils.info(logger, "modify", "id:" + id);
            BaseLogUtils.info(logger, "modify", "isOpen:" + isOpen);

            //主键id 存在修改
            if (StringUtils.isNotBlank(id)) {
                vo.setId(Long.valueOf(id));
                vo.setMdfTm(new Date());
                vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                BaseLogUtils.info(logger, "modify", "id 不为空，修改数据");
                this.sysFlowNoticeCfgService.modify(vo);
            } else {//主键id 存在新增
                vo.setSysCd(CurrentThreadContext.getCurrentSysCd());
                vo.setUsrId(CurrentThreadContext.getCurrentUserId());
                vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                BaseLogUtils.info(logger, "modify", "id 为空，新增数据");
                this.sysFlowNoticeCfgService.add(vo);
            }
            result.setSuccess(BaseConstant.IS_YESNO_YES);
            result.setInfo("设置成功");
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "modify", "设置失败.[id=" + id + "] DATA:" + JSONObject.toJSONString(vo) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "modify", "设置失败.[id=" + id + "] DATA:" + JSONObject.toJSONString(vo) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }
}

